// ui/settings.go
package ui

import (
	"device_manager/config"
	"device_manager/utils"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"gorm.io/gorm"
)

func CreateSettingsTab(db *gorm.DB, cfg *config.Config) fyne.CanvasObject {
	appIDEntry := widget.NewEntry()
	appIDEntry.SetText(cfg.AppID)
	appIDEntry.SetPlaceHolder("请输入 App ID")

	appSecretEntry := widget.NewEntry()
	appSecretEntry.SetText(cfg.AppSecret)
	appSecretEntry.SetPlaceHolder("请输入 App Secret")

	saveButton := widget.NewButton("保存", func() {
		cfg.AppID = appIDEntry.Text
		cfg.AppSecret = appSecretEntry.Text
		err := config.SaveConfig(db, cfg)
		if err != nil {
			utils.ShowError(err)
		} else {
			utils.ShowInfo("配置已保存")
		}
	})

	content := container.NewVBox(
		widget.NewForm(
			widget.NewFormItem("App ID", appIDEntry),
			widget.NewFormItem("App Secret", appSecretEntry),
		),
		saveButton,
	)

	return content
}
